package com.wsj.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsj.dao.IHomeSiteNewsCommentDao;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.page.PerfectPageResultSet;
import com.wsj.util.tool.StringUtils;
import com.wsj.util.tool.Tool;
import com.wsj.web.vo.homeSite.HomeSiteNewsComment;

public class HomeSiteNewsCommendDaoImpl extends HibernateDaoSupport implements
		IHomeSiteNewsCommentDao {

	private JdbcTemplate jdbcTemplate;
	
	public void del(Integer id) throws DataAccessException {
		if(id != null){
			this.jdbcTemplate.execute("delete from homeSite_news_comment where id = "+id);
		}
	}

	public void saveOrUpdate(HomeSiteNewsComment hsCommend) throws DataAccessException {
		if(hsCommend != null){
			this.getHibernateTemplate().saveOrUpdate(hsCommend);
		}
	}
	
	public HomeSiteNewsComment loadById(Integer id) throws DataAccessException {
		Object obj = null;
		HomeSiteNewsComment hs = null;
		obj = this.getHibernateTemplate().get(HomeSiteNewsComment.class, id);
		if(obj != null){
			hs = (HomeSiteNewsComment)obj;
		}
		return hs;
	}

	@SuppressWarnings("unchecked")
	public List<HomeSiteNewsComment> loadByNewsId(Integer newsId) throws DataAccessException {
		List list = null;
		String sql = "from HomeSiteNewsComment as h where h.homeSiteNews.id = ?";
		list = this.getHibernateTemplate().find(sql,newsId);
		if(StringUtils.isNotNull(list)){
			return list;
		}
		return null;
	}

	public PageResultSet loadPageList(Long newsId, Short isDel,
			Short audite, Integer page, Integer pageSize)
			throws DataAccessException {
		PageResultSet pageResultSet = new PageResultSet();
	    PerfectPageResultSet pageResult = new PerfectPageResultSet();
	    // 获取Session
	    Session session = SessionFactoryUtils.getSession(getSessionFactory(),true);
	    // 初始化SQL语句
	    StringBuffer sql = new StringBuffer( " from HomeSiteNewsComment as h  where 1=1 ");
	    
	    // 设置每页显示得记录数和开始页
	    pageResult.setPage(page, pageSize);

	    // 参数
	    List<Comparable> list = new ArrayList<Comparable>();
		
	    //  资讯ID
	    if(newsId != null && !new Integer(0).equals(0)){
			sql.append(" and h.homeSiteNews.id = ? ");
			list.add(newsId);
		}
	    
	    //  是否删除
	    if(isDel != null ){
			sql.append(" and h.isDel = ? ");
			list.add(isDel);
		}
	    
	    //  是否审核
	    if(audite != null ){
			sql.append(" and h.audite = 1 ");
			list.add(audite);
		}
	    
	    
	    sql.append(" order by deliverDatetime desc ");

	    // 把集合转换成数组
	    String[] strParams = Tool.toArray(list);
	    
	    pageResultSet = pageResult.pageResultSet(sql.toString(),session, strParams);
	    
	    return pageResultSet;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	

}
